*----------------------------------------------------------------------------------------------------------	* 
* RESEARCHERS:		EARN												   									*
* PROGRAMMED BY:	Linea Hasager																			*
* DESCRIPTION:		Sample attrition analysis.																*
*																											*
* CREATED:			Mar. 13, 2018																		   	*
* LAST MODIFIED:	May 25, 2022														       				*
*----------------------------------------------------------------------------------------------------------	*

clear 
set more off

// Directories
global in "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"
global output "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Output"
global temp "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Temp"
global dkdb "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"

*--------------------------------------------------------------------------------------------------------	

******************************
* 1) LOAD DATA
******************************

use "${in}\Analysis_sample.dta", replace




********************************************************************************
*FIRST PANEL: DIFFERENCE IN MEANS OUTMIGRATION FOR 1998 VS. 1999 COHORT
********************************************************************************

*MEAN
preserve
keep if year_admission>=1998 & year_admission<=1999 
egen mean=mean(outmig)
keep mean 
keep if _n==1
gen variable="outmig"
save "${temp}\outmig.dta", replace
restore

*DIFFERENCE IN MEANS
set more off
foreach var in outmig {
preserve
rdrobust `var' day_nr if year_admission>=1998 & year_admission<=1999,  p(0) h(1000) kernel(uni)
eststo `var'_e
regsave RD_Estimate using "${temp}\RD_outmig.dta", replace ci addlabel(variable,`var') 
restore
}


preserve
clear
use  "${temp}\outmig.dta"
merge 1:1 variable using "${temp}\RD_outmig.dta"
drop N var _merge


format mean  coef stderr ci_lower ci_upper %3.2fc



tostring ci_lower, gen(ci_l) force format(%3.2f)
tostring ci_upper, gen(ci_u) force format(%3.2f)

gen tt="["
gen ci= tt +ci_l + " ; " + ci_u + "]"
drop tt ci_l ci_u

replace variable="Left the Sample" if variable=="outmig"

*OUTPUT FIRST PANEL OF TABLE
listtex variable mean coef ci using "${output}\oa_table2a.tex", rstyle(tabular) replace
restore


******************************************************************************************
*SECOND PANEL, INDIVIDUAL CHARACTERISTICS ACROSS TREATMENT
******************************************************************************************

*MEAN
preserve
keep if year_admission>=1998 & year_admission<=1999  & outmig==1
egen mean=mean(age)
keep mean 
keep if _n==1
gen variable="age"
save "${temp}\attrition.dta", replace
restore

*MEAN
foreach var in married female kids02 kids018 iraq afghanistan other_country arabic dari danishf1 danishf2 danishf3 quota convention familyref other_permit {
preserve
keep if year_admission>=1998 & year_admission<=1999  & outmig==1
egen mean=mean(`var')
keep mean 
keep if _n==1
gen variable="`var'"
append using "${temp}\attrition.dta"
save "${temp}\attrition.dta", replace
restore
}

*DIFFERENCE IN MEANS
foreach var in age {
preserve
rdrobust `var' day_nr if year_admission>=1998 & year_admission<=1999  & outmig==1,  p(0) h(1000) kernel(uni)
eststo `var'_e
regsave RD_Estimate using "${temp}\RD_attrition.dta", replace ci addlabel(variable,`var') 
restore
}

*DIFFERENCE IN MEANS
foreach var in married female kids02 kids018 iraq afghanistan other_country arabic dari danishf1 danishf2 danishf3 quota convention familyref other_permit {
preserve
rdrobust `var' day_nr if year_admission>=1998 & year_admission<=1999 & outmig==1,  kernel(uni) c(0) p(0) h(1000)
eststo `var'_e
regsave RD_Estimate using "${temp}\RD_attrition.dta", append ci addlabel(variable, `var')
restore
}


preserve
clear
use  "${temp}\attrition.dta"
merge 1:1 variable using "${temp}\RD_attrition.dta"
drop N var _merge


format mean  coef stderr ci_lower ci_upper %3.2fc



tostring ci_lower, gen(ci_l) force format(%3.2f)
tostring ci_upper, gen(ci_u) force format(%3.2f)

gen tt="["
gen ci= tt +ci_l + " ; " + ci_u + "]"
drop tt ci_l ci_u

gen sortvar=1 if variable=="age"
replace sortvar=2 if variable=="married"
replace sortvar=4 if variable=="female"
replace sortvar=7 if variable=="kids02"
replace sortvar=8 if variable=="kids018"
replace sortvar=9 if variable=="iraq"
replace sortvar=10 if variable=="afghanistan"
replace sortvar=11 if variable=="other_country"
replace sortvar=12 if variable=="arabic"
replace sortvar=13 if variable=="dari"
replace sortvar=15 if variable=="danishf1"
replace sortvar=16 if variable=="danishf2"
replace sortvar=17 if variable=="danishf3"
replace sortvar=22 if variable=="quota"
replace sortvar=23 if variable=="convention"
replace sortvar=24 if variable=="familyref"
replace sortvar=25 if variable=="other_permit"


replace variable="Age" if variable=="age"
replace variable="Married" if variable=="married"
replace variable="Family-Reunified" if variable=="familyref"
replace variable="Female" if variable=="female"
replace variable="No. Children $<$ 3y" if variable=="kids02"
replace variable="No. Children 3-17y" if variable=="kids018"
replace variable="Iraq" if variable=="iraq"
replace variable="Afghanistan" if variable=="afghanistan"
replace variable="Other Country" if variable=="other_country"
replace variable="Speaks Arabic" if variable=="arabic"
replace variable="Speaks Dari" if variable=="dari"
replace variable="Danish 1" if variable=="danishf1"
replace variable="Danish 2" if variable=="danishf2"
replace variable="Danish 3" if variable=="danishf3"
replace variable="Quota Refugee" if variable=="quota"
replace variable="Convention Refugee" if variable=="convention"
replace variable="Other Refugee" if variable=="other_permit"

sort sortvar


*OUTPUT SECOND PANEL OF TABLE
listtex variable mean coef ci using "${output}\oa_table2b.tex", rstyle(tabular) replace

restore

erase "${temp}\RD_attrition.dta"
erase "${temp}\RD_outmig.dta"
erase "${temp}\attrition.dta"
erase "${temp}\outmig.dta"